import { message } from 'antd';
import api from '../../api';
import { getTreeList } from '../../utils/utils';
export default {
  namespace: 'portfolioDefinitionM', //组合定义
  state: {
    apprpvalUserList: [], //审批人
    orderUserList: [], //下达操作员
  },
  effects: {
    //查询产品列表
    *queryPortfolioPage({ payload, callback }, { call, put, select }) {
      const res = yield call(api.portfolioDefinition.queryPortfolioPage, payload);
      if (res && res.code == 0) {
        /**
                 * BOOKKEEPING_MODE: "簿记模式"
                    COMMAND_MODE: "指令模式"
                    VALUATION_BOOKKEEPING_MODEL: "估值系统对接模式"
                    VALUATION_TABLE_ANALYSIS_MODE: "估值表解析模式"
                */
        let obj = { BOOKKEEPING_MODE: 1, COMMAND_MODE: 2, VALUATION_BOOKKEEPING_MODEL: 3, VALUATION_TABLE_ANALYSIS_MODE: 4 };
        res.data?.dataSource?.map((item) => {
          item.tradeMode = item.tradeMode ? obj[item.tradeMode] : undefined;
        });
        callback(res.data);
      } else {
        callback();
        message.error(res ? res.msg || '查询产品列表失败!' : '查询产品列表失败!');
      }
    },
    //新增子节点
    *addNode({ payload, callback }, { call, put, select }) {
      const res = yield call(api.portfolioDefinition.addNode, payload);
      if (res && res.code == 0) {
        callback(res.data);
        message.success('新增子节点成功!');
      } else {
        callback();
        message.error(res ? res.msg || '新增子节点失败!' : '新增子节点失败!');
      }
    },
    //删除子节点
    *deleteNode({ payload, callback }, { call, put, select }) {
      const res = yield call(api.portfolioDefinition.deleteNode, payload);
      if (res && res.code == 0) {
        callback();
        message.success('删除成功!');
      } else {
        message.error(res ? res.msg || '删除失败!' : '删除失败!');
      }
    },
    //保存配置信息
    *saveSettings({ payload, callback }, { call, put, select }) {
      const res = yield call(api.portfolioDefinition.saveSettings, payload);
      if (res && res.code == 0) {
        callback();
        message.success('配置信息保存成功!');
      } else {
        message.error(res ? res.msg || '配置信息保存失败!' : '配置信息保存失败!');
      }
    },
    //查询组合树
    *queryTreeList({ payload, callback }, { call, put, select }) {
      const res = yield call(api.portfolioDefinition.queryTreeList, payload);
      if (res && res.code == 0) {
        const arr = [];
        getTreeList([res.data], arr);
        callback(arr);
      } else {
        callback();
        message.error(res ? res.msg || '查询组合树失败!' : '查询组合树失败!');
      }
    },
    //查询配置信息
    *queryConfigInfo({ payload, callback }, { call, put, select }) {
      const res = yield call(api.portfolioDefinition.queryConfigInfo, payload);
      if (res && res.code == 0) {
        if (res.data && !res.data.commandConfiguration) res.data.commandConfiguration = undefined;
        if (res.data && !res.data.investmentAdvisoryConfiguration) res.data.investmentAdvisoryConfiguration = undefined;
        callback(res.data);
      } else {
        callback();
        message.error(res ? res.msg || '查询配置信息失败!' : '查询配置信息失败!');
      }
    },
    //查询自动审批人
    *queryAprrovalUserList({ payload, callback }, { call, put, select }) {
      const res = yield call(api.portfolioDefinition.queryAprrovalUserList, payload);
      if (res && res.success) {
        const arr = [];
        res.data &&
          res.data.map((item) => {
            arr.push({
              title: item.name,
              value: item.userId,
            });
          });
        yield put({
          type: 'setState',
          payload: {
            apprpvalUserList: arr,
          },
        });
      } else {
        yield put({
          type: 'setState',
          payload: {
            apprpvalUserList: [],
          },
        });
        message.error(res ? res.mesg || '查询审批人失败' : '查询审批人失败!');
      }
    },
    //查询下达操作员
    *queryOrderUserList({ payload, callback }, { call, put, select }) {
      const res = yield call(api.portfolioDefinition.queryOrderUserList, payload);
      if (res && res.code == 0) {
        const arr = [];
        res.data &&
          res.data.map((item) => {
            arr.push({
              title: item.name,
              value: item.value,
            });
          });
        yield put({
          type: 'setState',
          payload: {
            orderUserList: arr,
          },
        });
      } else {
        yield put({
          type: 'setState',
          payload: {
            orderUserList: [],
          },
        });
        message.error(res ? res.msg || '查询下达操作员失败!' : '查询下达操作员失败!');
      }
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
